package org.example.easybuy.controller;

import com.github.pagehelper.PageInfo;
import com.mysql.cj.util.StringUtils;
import org.example.easybuy.entity.News;
import org.example.easybuy.service.NewsService;
import org.example.easybuy.util.CustomResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@RestController
public class NewsController {

    @Autowired
    private NewsService newsService;

    @RequestMapping("/getNewsPage")
    public CustomResult getNewsPage(@RequestParam Map<String,Object> params){
        CustomResult customResult=CustomResult.successResult();
        if(!StringUtils.isNullOrEmpty((String)params.get("title")))
        {
            String title="%"+params.get("title")+"%";
            params.put("title",title);
        }
        if(!StringUtils.isNullOrEmpty((String)params.get("createTime")))
        {
            String createTime="%"+params.get("createTime")+"%";
            params.put("createTime",createTime);
        }
        PageInfo<News> pageInfo=newsService.getNewsPageInfo(params);
        customResult.addResult("pageInfo",pageInfo);
        return customResult;
    }

    @GetMapping("/deleteNewsById")
    public CustomResult deleteNewsById(int id)
    {
        if(newsService.deleteNewsById(id))
        {
            return CustomResult.successResult().addResult("delMsg","yes");
        }
        return CustomResult.errorResult(500,"出错了").addResult("delMsg","no");
    }

    @RequestMapping("/updateNews")
    public CustomResult updateNews(@RequestParam Map<String,String> params)
    {
        News news=new News();
        news.setId(Integer.parseInt(params.get("id")));
        news.setTitle(params.get("title"));
        news.setContent(params.get("content"));
        news.setDelFlag(Integer.parseInt(params.get("delFlag")));
        if(newsService.updateNews(news))
        {
            return CustomResult.successResult().addResult("updateMsg","success");
        }else{
            return CustomResult.errorResult(500,"修改出错了").addResult("updateMsg","fail");
        }
    }


    @RequestMapping("/getNewsById")
    public CustomResult getNewsById(int id){
        News news= newsService.selectNewsById(id);
        return CustomResult.successResult().addResult("news",news);
    }


    @RequestMapping("/newsAdd")
    public CustomResult addNews(@RequestParam Map<String,String> params)
    {
        News news=new News();
        news.setTitle(params.get("title"));
        news.setContent(params.get("content"));
        news.setDelFlag(0);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        news.setCreateTime(format.format(new Date()));
        if(newsService.addNews(news))
        {
            return CustomResult.successResult().addResult("addMsg","success");
        }else{
            return CustomResult.errorResult(500,"添加出错了").addResult("addMsg","fail");
        }
    }

    @GetMapping("/checkTitle")
    public CustomResult checkTitle(String title)
    {
        News news=newsService.selectByTitle(title);
        if(news!=null)
        {
            return CustomResult.successResult().addResult("isCheck","yes").addResult("news",news);
        }
        else{
            return CustomResult.successResult().addResult("isCheck","no").addResult("news",news);
        }
    }

}
